package com.njust.RateMyTray_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njust.RateMyTray_backend.dto.NoticeCreateDTO;
import com.njust.RateMyTray_backend.entity.Notice;
import com.njust.RateMyTray_backend.entity.Notification;
import com.njust.RateMyTray_backend.entity.User;
import com.njust.RateMyTray_backend.mapper.NoticeMapper;
import com.njust.RateMyTray_backend.service.NoticeService;
import com.njust.RateMyTray_backend.service.NotificationService;
import com.njust.RateMyTray_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private UserService userService;

    @Autowired
    private NotificationService notificationService;

    @Transactional
    @Override
    public Notice createNoticeAndNotifyUsers(NoticeCreateDTO createDTO, Long publisherId) {
        // 1. 创建并保存公告
        Notice notice = new Notice();
        notice.setTitle(createDTO.getTitle());
        notice.setContent(createDTO.getContent());
        notice.setPublisherId(publisherId);
        notice.setPublishedAt(LocalDateTime.now());
        // 将目标角色列表转换为字符串存储，例如 "user,merchant"
        notice.setTargetRole(String.join(",", createDTO.getTargetRoles()));
        this.save(notice);

        // 2. 查找目标用户
        List<String> targetRoles = createDTO.getTargetRoles();
        if (CollectionUtils.isEmpty(targetRoles)) {
            return notice; // 如果没有目标角色，直接返回
        }

        LambdaQueryWrapper<User> userQuery = new LambdaQueryWrapper<>();
        if (targetRoles.contains("all")) {
            // "all" 意味着所有角色，所以不需要添加角色条件
        } else {
            userQuery.in(User::getRole, targetRoles);
        }
        List<User> targetUsers = userService.list(userQuery);

        // 3. 为目标用户批量创建通知
        if (!CollectionUtils.isEmpty(targetUsers)) {
            List<Notification> notifications = targetUsers.stream().map(user -> {
                Notification notification = new Notification();
                notification.setUserId(user.getId());
                notification.setType("systemNotice");
                notification.setContent(notice.getTitle()); // 通知内容为公告标题
                notification.setSourceId(notice.getId());
                notification.setIsRead(false);
                notification.setCreatedAt(LocalDateTime.now());
                return notification;
            }).collect(Collectors.toList());
            notificationService.saveBatch(notifications);
        }

        return notice;
    }
} 